package com.hmi.simulatedrive.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.amap.api.navi.AMapNavi
import com.amap.api.navi.AMapNaviViewOptions
import com.amap.api.navi.enums.NaviType
import com.amap.api.navi.model.AMapCalcRouteResult
import com.hmi.simulatedrive.databinding.FragmentAmapviewBinding
import com.hmi.simulatedrive.lifecycle.AMapViewLifecyleObserve

class AMapViewFragment: BaseNaviMapFragment() {
    var fragmentAmapviewBinding: FragmentAmapviewBinding? = null
    val aMapView by lazy { fragmentAmapviewBinding?.naviView }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 导航使用内置语音
        mAMapNavi.setUseInnerVoice(true, true)
        // aMapNavi添加监听
        mAMapNavi.addAMapNaviListener(AMapViewFragment@this)
        // 设置起终点，在onInitNaviSuccess()方法中开始计算路径
        initStartAndEndPoint()
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        fragmentAmapviewBinding = FragmentAmapviewBinding.inflate(inflater, container, false)
        fragmentAmapviewBinding!!.naviView.onCreate(savedInstanceState)
        // 添加生命周期监听
        lifecycle.addObserver(AMapViewLifecyleObserve(fragmentAmapviewBinding!!.naviView))
        val options = AMapNaviViewOptions()
        options.tilt = 0
        options.isLayoutVisible = false
        fragmentAmapviewBinding!!.naviView.viewOptions = options
        fragmentAmapviewBinding!!.naviView.setAMapNaviViewListener(AMapViewFragment@this)
        return fragmentAmapviewBinding!!.root
    }

    override fun onInitNaviSuccess() {
        super.onInitNaviSuccess()
        /**
         * 方法: int strategy=mAMapNavi.strategyConvert(congestion, avoidhightspeed, cost, hightspeed, multipleroute); 参数:
         *
         * @congestion 躲避拥堵
         * @avoidhightspeed 不走高速
         * @cost 避免收费
         * @hightspeed 高速优先
         * @multipleroute 多路径
         *
         * 说明: 以上参数都是boolean类型，其中multipleroute参数表示是否多条路线，如果为true则此策略会算出多条路线。
         * 注意: 不走高速与高速优先不能同时为true 高速优先与避免收费不能同时为true
         */
        var strategy = 0
        try {
            //再次强调，最后一个参数为true时代表多路径，否则代表单路径
            strategy = mAMapNavi.strategyConvert(true, false, false, false, false)
        } catch (e: Exception) {
            e.printStackTrace()
        }
        // 初始化成功后开始计算路径
        mAMapNavi.calculateDriveRoute(sList, eList, mWayPointList, strategy)
    }

    /**
     *
     * */
    override fun onCalculateRouteSuccess(aMapCalcRouteResult: AMapCalcRouteResult?) {
        super.onCalculateRouteSuccess(aMapCalcRouteResult)
        mAMapNavi.startNavi(NaviType.EMULATOR)
    }

    private fun initStartAndEndPoint() {
        sList.add(mStartLatlng)
        eList.add(mEndLatlng)
    }

    override fun onDestroy() {
        super.onDestroy()
        // fragment销毁，则停止导航
        //since 1.6.0 不再在naviview destroy的时候自动执行AMapNavi.stopNavi();请自行执行
        if (mAMapNavi != null) {
            mAMapNavi.stopNavi()
            AMapNavi.destroy()
        }
    }
}